// MemFree.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
/*return the operation should be persumed*/
const int MemCount = 400;
const int BlockSize = 10L * 1024 * 1024;
int CalcMemory()
{
MEMORYSTATUS mms;
memset(&mms,0,sizeof(mms));
mms.dwLength = sizeof(mms);
GlobalMemoryStatus(&mms);
mms.dwTotalPhys /= BlockSize;
mms.dwTotalPhys = mms.dwTotalPhys * 2 / 3 + 2; //
return (mms.dwTotalPhys==0||mms.dwTotalPhys>=MemCount) ? 15 : (int)mms.dwTotalPhys;
}
void DoFreeMemory()
{
int nToDo = CalcMemory();
int nCount = 0;
char* ppMem[MemCount+1];
char* pTmp;
/*init pointer to zero*/
memset(ppMem,0,sizeof(char*) * MemCount);
/*alloc memory*/
while(nCount<nToDo)
{
pTmp = new char[BlockSize];
if(pTmp==NULL)break;
ppMem[nCount++] = pTmp;
memset(pTmp,0,BlockSize);
Sleep(200);
}
/*free memory*/
nCount = 0;
while(nCount<nToDo)
{
if(ppMem[nCount])delete[] ppMem[nCount];
nCount++;
}
/*job done!*/
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HANDLE hFreer = CreateMutex(NULL,FALSE,"BitSpirit - Memory - Freer");
if(hFreer==NULL||GetLastError()==ERROR_ALREADY_EXISTS)return 1;//failed or engaged
DoFreeMemory();
CloseHandle(hFreer);
return 0;
}
|